package com.sunjob.mrotech.server.Impl;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.sunjob.common.domain.loginServer.SysUser;
import com.sunjob.common.domain.tech.*;
import com.sunjob.common.util.MROUtils;
import com.sunjob.mrotech.mapper.RepairProcessMapper;
import com.sunjob.mrotech.server.RepairProcessServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @author : xiaozhoutongxue
 * @date : 2022/8/19 17:47
 */
@Service
public class RepairProcessImpl implements RepairProcessServer {
    @Autowired
    RepairProcessMapper repairProcessMapper;

    public String getUserName() {
        SaSession tokenSession = StpUtil.getTokenSession();
        SysUser sysUser = (SysUser) tokenSession.get("userInfo");
        return sysUser.getUserName();
    }

    @Override
    public List<TransShape> getRepairProcessByLimit3() {
        return repairProcessMapper.getRepairProcessByLimit3();
    }

    @Override
    public RepairProcess getRepairProcessByShapeId(Long shapeId) {
        return repairProcessMapper.getRepairProcessByShapeId(shapeId);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public String insertRepairProcess(RepairProcess repairProcess) {
        try {
            if(repairProcess.getSteps().size()==0){
                return "至少有一个步骤才行哦";
            }
            repairProcess.setRepairProcessId(MROUtils.nextId());
            for (int i = 0; i < repairProcess.getSteps().size(); i++) {
                RepairProcessDO repairProcessDO = new RepairProcessDO();
                repairProcessDO.setRepairProcessId(repairProcess.getRepairProcessId());
                repairProcessDO.setRepairProcessName(repairProcess.getRepairProcessName());
                repairProcessDO.setStep(repairProcess.getSteps().get(i).getOrder());
                repairProcessDO.setText(repairProcess.getSteps().get(i).getText());
                repairProcessDO.setPicId(repairProcess.getSteps().get(i).getPicId());
                repairProcessDO.setVersion(1L);
                repairProcessDO.setCreateBy(getUserName());
                repairProcessDO.setCreateTime(LocalDateTime.now());
                repairProcessMapper.insertRepairProcessById(repairProcessDO);
            }
        } catch (Exception e) {
            return "数据增加失败";
        }
        return "true";
    }

    @Override
    public Boolean bindRepairProcessByShapeId(Long shapeId, Long repairProcessId) {
        int i = repairProcessMapper.bindRepairProcessByShapeId(shapeId, repairProcessId, getUserName(), LocalDateTime.now());
        return i > 0 ? true : false;
    }

    @Override
    public String deleteRepairProcessById(Long repairProcessId) {
        RepairProcess repairProcess = repairProcessMapper.getRepairProcessById(repairProcessId);
        if (repairProcess == null) {
            return "未找到该修程工艺";
        }
        Long version = repairProcess.getVersion();
        int i = repairProcessMapper.deleteRepairProcessById(repairProcessId, version, getUserName(), LocalDateTime.now());
        return i > 0 ? "true" : "删除失败";
    }

    @Override
    public Boolean uptateRepairProcessSmall(RepairProcessDO repairProcessDO1) {
        RepairProcessDO repairProcessDO =new RepairProcessDO();
        repairProcessDO.setRepairProcessId(repairProcessDO1.getRepairProcessId());
        repairProcessDO.setStep(repairProcessDO1.getStep());
        repairProcessDO.setVersion(repairProcessDO1.getVersion());
        repairProcessDO.setText(repairProcessDO1.getText());
        repairProcessDO.setPicId(repairProcessDO1.getPicId());
        repairProcessDO.setUpdateBy(getUserName());
        repairProcessDO.setStatus(0);
        repairProcessDO.setUpdateTime(LocalDateTime.now());
        int i = repairProcessMapper.uptateRepairProcessSmall(repairProcessDO);
        return i > 0 ? true : false;
    }

    @Override
    @Transactional
    public Boolean uptateRepairProcessBig(RepairProcess newRepairProcess) {
        //得到原来数据库的信息并且更新状态
        Long repairProcessId = newRepairProcess.getRepairProcessId();
        RepairProcess oldRepairProcess = repairProcessMapper.getRepairProcessById(repairProcessId);
        for (int i = 0; i < oldRepairProcess.getSteps().size(); i++) {
            RepairProcessDO repairProcessDO = new RepairProcessDO();
            repairProcessDO.setRepairProcessId(oldRepairProcess.getRepairProcessId());
            repairProcessDO.setStep(i+1);
            repairProcessDO.setVersion(oldRepairProcess.getVersion());
            repairProcessDO.setText(oldRepairProcess.getSteps().get(i).getText());
            repairProcessDO.setPicId(oldRepairProcess.getSteps().get(i).getPicId());
            repairProcessDO.setStatus(1);
            repairProcessDO.setUpdateBy(getUserName());
            repairProcessDO.setUpdateTime(LocalDateTime.now());
            repairProcessMapper.uptateRepairProcessSmall(repairProcessDO);
        }
        //增加新的版本号的数据
        Long newVersion = oldRepairProcess.getVersion() + 1;
        for (int i = 0; i < newRepairProcess.getSteps().size(); i++) {
            RepairProcessDO repairProcessDO = new RepairProcessDO();
            repairProcessDO.setRepairProcessId(newRepairProcess.getRepairProcessId());
            repairProcessDO.setRepairProcessName(oldRepairProcess.getRepairProcessName());
            repairProcessDO.setStep(newRepairProcess.getSteps().get(i).getOrder());
            repairProcessDO.setText(newRepairProcess.getSteps().get(i).getText());
            repairProcessDO.setPicId(newRepairProcess.getSteps().get(i).getPicId());
            repairProcessDO.setVersion(newVersion);
            repairProcessDO.setCreateBy(oldRepairProcess.getCreateBy());
            repairProcessDO.setCreateTime(oldRepairProcess.getCreateTime());
            repairProcessDO.setUpdateBy(getUserName());
            repairProcessDO.setUpdateTime(LocalDateTime.now());
            repairProcessDO.setStatus(0);
            repairProcessMapper.insertRepairProcessById(repairProcessDO);
        }
        return true;
    }
}
